<?php
//基本设置
if(!defined('IN_T')){
   die('hacking attempt');
}

if ($act == "index") {
	//查看信息
	if($company['p_id']>0){
		$c_id = $company['p_id'];
	}
	else{
		$c_id = $company['id'];
	}
	
	//基本设置或支付设置
	$type = Common::sfilter($_REQUEST['type']) ? Common::sfilter($_REQUEST['type']) :  'profile';
	
	//检测数据，判断合法性
	$row = $Db->query("select * from ".$Base->table('company_'.$type.'')." where c_id=$c_id","Row");
	if(empty($row)){
		$id = 0;
	}
	else{
		$id = $row['id'];
		$open_time = explode(',',$row['open_time']);
		$row['open_start'] = $open_time[0];
		$row['open_end'] = $open_time[1];
	}
	
	if(empty($row['position'])){
		$row['position'] ='104.852528,26.592354';
	}
	
	if($type == "profile"){
		//前台显示
		if(empty($_POST)){
			//print_r($row);
			$tp->assign('profile',$row);
		}
		//数据处理
		else{
			$res['status'] = 0;
			$data = array(
				'c_id'=>$c_id,
				'name'=>Common::sfilter($_POST['name']),
				'phone'=>Common::sfilter($_POST['phone']),
				'avatar'=>Common::sfilter($_POST['avatar']),
				'brief'=>Common::sfilter($_POST['content'],'html'),
				'address'=>Common::sfilter($_POST['address']),
				'position'=>Common::sfilter($_POST['position']),
				'lowest'=>intval($_POST['lowest']),
				'open_time'=>intval($_POST['start']).','.intval($_POST['end']),
				'contact'=>Common::sfilter($_POST['contact']),
				'contact_position'=>Common::sfilter($_POST['contact_position']),
				'contact_qrcode'=>Common::sfilter($_POST['contact_qrcode']),
				'is_book'=>intval($_POST['is_book']),
				'notice'=>Common::sfilter($_POST['notice']),
				'href'=>Common::sfilter($_POST['href']),
				'consume_tip'=>Common::sfilter($_POST['consume_tip']),
				'is_lowest'=>Common::sfilter($_POST['is_lowest']),
				'book_href'=>Common::sfilter($_POST['book_href']),
				);
			if(empty($data['name'])){
				$res['msg'] = '企业名称不能为空！';
			}
			else if(empty($data['phone'])){
				$res['msg'] = '联系电话不能为空！';
			}
			else if(empty($data['avatar'])){
				$res['msg'] = '企业LOGO不能为空！';
			}
			else if(empty($data['contact'])){
				$res['msg'] = '联系人不能为空';
			}
			else if(empty($data['contact_position'])){
				$res['msg'] = '联系人职位不能为空';
			}
			else if(empty($data['contact_qrcode'])){
				$res['msg'] = '请上传微信二维码';
			}
			else if(empty($data['brief'])){
				$res['msg'] = '企业简介不能为空！';
			}
			else if(empty($data['address'])){
				$res['msg'] = '企业地址不能为空！';
			}
			else if(empty($data['position'])){
				$res['msg'] = '企业坐标不能为空！';
			}
            else if (!empty($data["notice"]) && (mb_strlen($data["notice"]) < 0 || mb_strlen($data["notice"]) > 125)) {
                $re['msg'] = "通知请输入1到125个字符长度";
            } else if (!empty($data["href"]) && (mb_strlen($data["href"]) < 0 || mb_strlen($data["href"]) > 125)) {
                $re['msg'] = "链接请输入1到125个字符长度";
            }
            else if (empty($data["consume_tip"]) || mb_strlen($data["consume_tip"]) < 0 || mb_strlen($data["consume_tip"]) > 30) {
                $re['msg'] = "消费提示请输入1到30个字符长度";
            }
            else{
				if($id){
					$Db->update($Base->table('company_profile'),$data,array('id'=>$id));
				}
				else{
					$Db->insert($Base->table('company_profile'),$data);
				}
				$res = array('status'=>'1','msg'=>'提交成功');
			}
			echo $Json->encode($res);
			exit;
		}
	}
	else if($type == "payment"){
		//前台显示
		if(empty($_POST)){
			$tp->assign('pay',$row);
		}
		//数据处理
		else{
			$res['status'] = 0;
			$data = array(
				'id'=>$id,
				'c_id'=>$c_id,
				'appid'=>Common::sfilter($_POST['appid']),
				'appsecret'=>Common::sfilter($_POST['appsecret']),
				'key'=>Common::sfilter($_POST['key']),
				'mchid'=>Common::sfilter($_POST['mchid']),
				);
			// var_dump($data);die;
			if(empty($data['appid'])){
				$res['msg'] = 'appid不能为空！';
			}
			else if(empty($data['appsecret'])){
				$res['msg'] = 'appsecret不能为空！';
			}
			else if(empty($data['key'])){
				$res['msg'] = '商户支付密钥不能为空！';
			}
			else if(empty($data['mchid'])){
				$res['msg'] = '商户号不能为空！';
			}
			else{
				$Db->replace($Base->table('company_payment'),$data);
				$res = array('status'=>'1','msg'=>'提交成功');
			}
			echo $Json->encode($res);
			exit;
		}
	}
	
}
$tp->assign('type',$type);
$tp->assign('act',$act);
$tp->assign('nav','企业设置');

?>